home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / fault / fault.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-13  |  3.8 KB  |  175 lines

  1. /* 
  2.  * quick test program to cause an address fault. 
  3.  * 
  4.  * $Header: /user5/kupfer/spriteserver/tests/fault/RCS/fault.c,v 1.3 92/03/12 20:47:29 kupfer Exp $
  5.  */
  6.  
  7. /* 
  8.  * usage: fault [ stack|readonly|ignore|handle ] 
  9.  * 
  10.  * where "stack" means to touch enough pages to test the stack-extension 
  11.  * code in Sprite, "readonly" means to try writing read-only memory, and 
  12.  * "ignore" means to disable receipt of the signal.  Only one option can be 
  13.  * specified. 
  14.  * 
  15.  * Should probably not be built with optimization turned on.
  16.  */
  17.  
  18. #include <sprite.h>
  19. #include <mach.h>
  20. #include <setjmp.h>
  21. #include <signal.h>
  22. #include <status.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <test.h>
  27. #include <vm.h>
  28.  
  29. jmp_buf jmpBuf;            /* state saved by setjmp */
  30. int numFaults = 0;        /* number of times we caused an exception */
  31. char globalCh;            /* sometimes used by signal handler */
  32.  
  33. void DoStack();
  34. void DoReadOnly();
  35. void SegvHandler(), FpeHandler();
  36.  
  37. int
  38. main(argc, argv)
  39.     int argc;
  40.     char *argv[];
  41. {
  42.     char *bogusPtr = (char *)0x23456789; /* 0x10000000 is heap on DECstation */
  43.     char ch;
  44.  
  45.     if (argc > 1) {
  46.     if (strcmp(argv[1], "stack") == 0) {
  47.         (void)DoStack((int)vm_page_size * 2);
  48.         exit(0);
  49.     } else if (strcmp(argv[1], "readonly") == 0) {
  50.         DoReadOnly();
  51.         exit(0);
  52.     } else if (strcmp(argv[1], "ignore") == 0) {
  53.         if (signal(SIGSEGV, SIG_IGN) == BADSIG) {
  54.         perror("can't ignore SIGSEGV");    /* XXX not allowed by Sprite */
  55.         exit(1);
  56.         }
  57.     } else if (strcmp(argv[1], "handle") == 0) {
  58.         if (signal(SIGSEGV, SegvHandler) == BADSIG) {
  59.         perror("Can't register SIGSEGV handler");
  60.         exit(1);
  61.         }
  62.         if (signal(SIGFPE, FpeHandler) == BADSIG) {
  63.         perror("Can't register SIGFPE handler");
  64.         exit(1);
  65.         }
  66.     }
  67.     }
  68.  
  69.     if (setjmp(jmpBuf)) {
  70.     Test_PutMessage("longjmp\n");
  71.     }
  72.     if (numFaults > 5) {
  73.     exit(numFaults);
  74.     }
  75.  
  76.     ch = *bogusPtr;
  77.     Test_PutMessage("it worked?\n");
  78.     return ch;            /* prevent optimizer from interfering */
  79. }
  80.  
  81. /* 
  82.  * Recurse a bunch of times to force automatic growing of the stack. 
  83.  */
  84. void
  85. DoStack(numTimes)
  86.     int numTimes;        /* number of times to recurse */
  87. {
  88.     int foo;            /* force something to go on the stack */
  89.  
  90.     foo = numTimes;
  91.     if (numTimes > 0) {
  92.     (void)DoStack(numTimes - 1);
  93.     }
  94. #ifdef lint
  95.     foo = foo;
  96. #endif
  97. }
  98.  
  99. /* 
  100.  * Map a file read-only then try to write to it.  The if'd out code has 
  101.  * interesting addressing errors you can get (some only happen on certain 
  102.  * machine types).
  103.  */
  104. void
  105. DoReadOnly()
  106. {
  107.     ReturnStatus status;
  108.     Address startAddr;
  109.     char *fileName = "testInput";
  110.     int foo = 0;
  111.  
  112.     status = Vm_MapFile(fileName, TRUE, (off_t)0, 1, &startAddr);
  113.     if (status != SUCCESS) {
  114.     Test_PutMessage("Couldn't map `");
  115.     Test_PutMessage(fileName);
  116.     Test_PutMessage("': ");
  117.     Test_PutMessage(Stat_GetMsg(status));
  118.     Test_PutMessage("\n");
  119.     return;
  120.     }
  121.  
  122. #if 0
  123.     foo = *(int *)(startAddr + 1); /* force word reference to unaligned addr */
  124.     *(int *)(startAddr+1) = 42;    /* ditto */
  125. #endif
  126.     *(int *)startAddr = 42;
  127.  
  128. #ifdef lint
  129.     foo = foo;
  130. #endif
  131. }
  132.  
  133. void
  134. SegvHandler(sigNum, code, contextPtr, addr)
  135.     int sigNum, code;
  136.     Address contextPtr, addr;
  137. {
  138. #if 0
  139.     printf("(%d, %d, 0x%x, 0x%x)\n", sigNum, code, contextPtr, addr);
  140.     exit(0);
  141. #endif
  142. #if 0
  143.     ++numFaults;
  144.     longjmp(jmpBuf, 1);
  145. #endif
  146. #if 0
  147.     /* Cause another instance of the same exception. */
  148.     globalCh = *(char *)-1;
  149.     Test_PutMessage("SegvHandler: shouldn't get here.\n");
  150.     exit(1);
  151. #endif
  152.     /* Cause a different exception. */
  153.     double foo, bar;
  154.  
  155.     foo = 0.0;
  156.     bar = 0.0;
  157.     printf("%f\n", foo/bar);
  158.     Test_PutMessage("SegvHandler: shouldn't get here.\n");
  159.     exit(1);
  160.  
  161. #ifdef lint
  162.     sigNum = sigNum;
  163.     code = code;
  164.     contextPtr = contextPtr;
  165.     addr = addr;
  166. #endif
  167. }
  168.  
  169. void
  170. FpeHandler()
  171. {
  172.     ++numFaults;
  173.     longjmp(jmpBuf, 1);
  174. }
  175.